/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tp2pdf.entity;

import tp2pdf.entity.entity.Compte;
import tp2pdf.entity.entity.Utilisateur;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import tp2pdf.entity.dao.UtilisateurDaoInterface;
import tp2pdf.entity.dao.UtilisateurDao;
import tp2pdf.entity.ejb.Gestion;

/**
 *
 * @author Danicela
 */
@WebServlet(name="EntityServlet", urlPatterns={"/EntityServlet"})
public class EntityServlet extends HttpServlet {

    @EJB
    Gestion gestion;
   
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");

        //Aiguilleur

        String action = request.getParameter("action");
        String forward = null;
        //try {
        if(action == null)
        {
            forward = "Menu";
        }

        else if(action.equals("addUtil")) {
                forward = addUtil(request);
            } 
        else if (action.equals("getUtil"))
            {
                forward = getUtil(request);
            }
        else if (action.equals("addCompte"))
            {
                forward = addCompte(request);
            }
        else if (action.equals("getComptes"))
            {
                forward = getComptes(request);
            }
         else if (action.equals("modifyUtil"))
            {
                forward = modifyUtil(request);
            }
            /*else //Si l'action est inconnue
            {
            forward = erreur(request);
            }
            } catch (Exception e) {
            forward = erreur();
            request.setAttribute("erreur", e.getMessage());
            }*/

            request.getRequestDispatcher(forward + ".jsp").forward(request, response);


        }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

/*    private void initBDManagerContext()
    {
        try{//disparait, on appele dans la servlet directement l'ejb
        Context context = new InitialContext();
        bd = (BDInterface) context.lookup("java:module/BDManager");
        }
        catch(NamingException e){System.err.println("naming!!!");}
    }*/

    private String addUtil(HttpServletRequest request)
    {
        String login = request.getParameter("login");
        String mdp = request.getParameter("mdp");

        gestion.add(new Utilisateur(login, mdp));

        return "Menu";
    }

    private String getUtil(HttpServletRequest request)
    {
        String login = request.getParameter("login");
       
        Utilisateur u = gestion.get(login);

        request.setAttribute("util", u);

        return "GetUtilResult";
    }

    private String addCompte(HttpServletRequest request)
    {
        String loginUtilisateur = request.getParameter("login");
        String nom = request.getParameter("nom");
       
        gestion.addCompte(new Compte(new Utilisateur(loginUtilisateur), nom));

        return "Menu";

    }

    private String getComptes(HttpServletRequest request)
    {
        String loginUtilisateur = request.getParameter("login");

        List<Compte> lc = gestion.getComptes(loginUtilisateur);

        request.setAttribute("comptes", lc);

        return "GetComptesResult";

    }

    private String modifyUtil(HttpServletRequest request)
    {
        String loginOld = request.getParameter("login_old");
        String loginNew = request.getParameter("login_new");

        /*initUtilisateurContext();
        Utilisateur u =  ui.getUtil(loginOld);

        u.setLogin(loginNew);*/
        //ui.flush();

        return "Menu";

    }

    private String erreur()
    {
        return "erreur";
    }

}
